package com.tmsps.auto.utils;

import java.security.MessageDigest;

/**
 * MD5 算法
 * 
 * @author zhangwei
 * 
 * 
 * @time 2014-3-27 下午3:31:46
 * 
 */
public class MD5 {
	/**
	 * 32位MD5加密算法
	 * 
	 * @param inStr
	 * @return
	 */
	public static String md5(String inStr) {
		if (inStr == null || inStr.equals("")) {
			return "";
		}
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];

		byte[] md5Bytes = md5.digest(byteArray);

		StringBuilder hexValue = new StringBuilder();

		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}

		return hexValue.toString();
	}

	public static void main(String args[]) {
		System.err.println(org.ne.framework.data.util.GenerateTools.getBase58ID());
		System.out.println(MD5.md5("admin"));
	}
}
